package com.zhuzi.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;

/**
 * @author zhuzi
 * 2023/7/8 22:55
 */
@Slf4j
public class HttpClientUtils {
    /**
     * http post
     *
     * @param content 内容
     * @param url     url
     */
    public static void httpPost(String content, String url) {
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
            HttpPost request = new HttpPost(url);
            StringEntity params = new StringEntity(content, StandardCharsets.UTF_8);
            request.addHeader("content-type", "application/json");
            request.setEntity(params);
            CloseableHttpResponse response = httpClient.execute(request);
            String res = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
            httpClient.close();
            log.info("WX机器人-返回消息返回[{}]", res);
        } catch (Exception e) {
            log.error("WX机器人-发送消息错误", e);
        }
    }
}
